package cn.pzx.devbase

import android.os.Handler
import android.util.SparseArray
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import cn.pzx.base_module.base.BaseActivity
import cn.pzx.devbase.databinding.ActivityMainBinding
import cn.pzx.devbase.vm.MainVM
import cn.pzx.main_module.ui.activity.PostActivity
import cn.pzx.main_module.ui.fragment.ClassifyFragment
import cn.pzx.main_module.ui.fragment.MainFragment
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.next.easynavigation.view.EasyNavigationBar
import com.next.easynavigation.view.EasyNavigationBar.OnCenterTabSelectListener
import com.next.easynavigation.view.EasyNavigationBar.OnTabClickListener
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch


class MainActivity : BaseActivity<MainVM, ActivityMainBinding>() {

    private val tabText = arrayOf("首页", "消息", "发现", "我的")

    //未选中icon
    private val normalIcon =
        intArrayOf(
            R.mipmap.icon_home_unselect,
            R.mipmap.icon_message_unselect,
            R.mipmap.icon_discover_unselect,
            R.mipmap.icon_me_unselect
        )

    //选中时icon
    private val selectIcon =
        intArrayOf(
            R.mipmap.icon_home,
            R.mipmap.icon_message,
            R.mipmap.icon_discover,
            R.mipmap.icon_me
        )


    var flag = false//是否旋转

    val mHandler: Handler = Handler()

    val fragments: SparseArray<Fragment> = SparseArray<Fragment>()
    override fun initView() {
        fragments.put(FPAdapterAdapter.PAGE_HOME, MainFragment()
            .apply {

            name = "aaaaa"


        })
        fragments.put(FPAdapterAdapter.PAGE_FIND, ClassifyFragment()
            .apply {

//            name = "bbbb"


        })
        fragments.put(FPAdapterAdapter.PAGE_INDICATOR, MainFragment()
            .apply {

            name = "cccc"


        })
        fragments.put(FPAdapterAdapter.PAGE_OTHERS, MainFragment()
            .apply {

            name = "dddd"


        })

        val adapter = FPAdapterAdapter(this, fragments)

        vb.mainViewpager.adapter = adapter
        vb.mainViewpager.isUserInputEnabled = false
        vb.mainViewpager.offscreenPageLimit = 4
        LogUtils.e(this.resources.getDimension(R.dimen.dp_100))
        vb.navigationBar.setupWithViewPager(vb.mainViewpager)
        vb.navigationBar.titleItems(tabText)
            .normalIconItems(normalIcon)
            .selectIconItems(selectIcon)
//            .fragmentList(fragments)
            .centerImageRes(R.mipmap.icon_post)
            .centerTextStr("发布")
//            .anim(null)
            .centerLayoutRule(EasyNavigationBar.RULE_BOTTOM)
            .centerLayoutBottomMargin(0)
            .centerAlignBottom(true)
//            .fragmentManager(supportFragmentManager)
            .setOnTabClickListener(object : OnTabClickListener {
                override fun onTabSelectEvent(view: View?, position: Int): Boolean {
                    return false
                }

                override fun onTabReSelectEvent(view: View?, position: Int): Boolean {
                    return false
                }
            })
            .setOnCenterTabClickListener(OnCenterTabSelectListener {
                mHandler.post(Runnable { //＋ 旋转动画
                    if (flag) {
                        vb.navigationBar.getCenterImage().animate().rotation(45f).setDuration(400)
                        ActivityUtils.startActivity(PostActivity::class.java)
                    } else {
                        vb.navigationBar.getCenterImage().animate().rotation(0f).setDuration(400)
                    }
                    flag = !flag
                })
                false
            })
            .canScroll(true)
            .mode(EasyNavigationBar.NavigationMode.MODE_ADD)
            .build()


//        vb.mainUserTabGroup.setViewPager(vb.mainViewpager)

    }

    override fun initClick() {
//        vb.mainViewpager.currentItem=1

    }

    override fun initData() {
//        GlobalScope.launch{}

    }

    override fun initVM() {


//        vm.bannerList.observe(this, Observer {
//            LogUtils.e(
//                it.datas!![0].toString()
//            )
//        })
    }


    class FPAdapterAdapter(fragmentActivity: FragmentActivity, list: SparseArray<Fragment>) :
        FragmentStateAdapter(fragmentActivity) {

        private var mFragments = SparseArray<Fragment>()

        init {
            mFragments = list

        }

        override fun createFragment(position: Int): Fragment {
            return mFragments[position] as Fragment
        }

        override fun getItemCount(): Int {
            return mFragments.size()
        }

        companion object {

            const val PAGE_HOME = 0

            const val PAGE_FIND = 1

            const val PAGE_INDICATOR = 2

            const val PAGE_OTHERS = 3

        }
    }


}


